package our.pack;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.AdapterContextMenuInfo;


public class MediaCameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
	private static final int FOTO_MODE = 0;
	private static final String TAG = "Camera";
	
	private Camera camera;
	private boolean previewRunning = false;
	private boolean pictureTaken = false;
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private byte[] imageByteData;
	private MyPoint point;

	/**
	 * makes a picture and shows a preview on the surfaceView
	 */

	public void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);

		Log.d(TAG, "onCreate");

		//get the screen right
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.mediafoto);
		
		//sets up the surfaceView and surfaceHolder
		surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		surfaceView.setOnClickListener(this);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		//get the point where it's made
		Intent i = getIntent();
		Bundle b = i.getExtras();
		point = (MyPoint) b.get("Point");
	}

	//if people make a photo
	Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
		public void onPictureTaken(byte[] imageData, Camera c) {

			if (imageData != null) {

				imageByteData = imageData; 
				surfaceHolder.getSurface().freeze();
				pictureTaken = true;
							}
		}
	};

	//when surfaceView is created it opens the camera
	public void surfaceCreated(SurfaceHolder holder) {
		Log.e(TAG, "surfaceCreated");
		camera = Camera.open();

	}

	//if the surface changes the preview is remade with the right size
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		Log.e(TAG, "surfaceChanged");

		// XXX stopPreview() will crash if preview is not running
		if (previewRunning) {
			camera.stopPreview();
		} else {

		Camera.Parameters p = camera.getParameters();
		p.setPreviewSize(w, h);
		camera.setParameters(p);
		try {
			camera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		camera.startPreview();
		previewRunning = true;
		}
	}

	//when surfaceView is destroyed stop everything
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.e(TAG, "surfaceDestroyed");
		camera.stopPreview();
		previewRunning = false;
		camera.release();
	}

	//when people click they take a picture, if they made a picture it gets to the save Activity
	public void onClick(View arg0) {

		if(pictureTaken){
			Intent i = new Intent(MediaCameraActivity.this, MediaCameraSaveActivity.class);
			i.putExtra("Photo", imageByteData);
			i.putExtra("Point", point);
			startActivity(i);
			
		} else {
			camera.takePicture(null, mPictureCallback, mPictureCallback);
		}
	}
}